home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
12984
/
12984.xpi
/
chrome
/
VideoDownloaderToolbar.jar
/
content
/
facebook.js
< prev
next >
Wrap
Text File
|
2010-01-29
|
10KB
|
376 lines
if(!com) var com={};
if(!com.VidBar) com.VidBar={};
com.VidBar.VidFB = {
FB_ROOT: 'http://www.facebook.com/',
FB_API: 'http://api.facebook.com/restserver.php',
pref : null,
xmlHttp : null,
api_key : 'f883215e0f13977f4c4f8019876fdc96',
fsecret_key : 'f7bcacb9f8692e7ac235063cd9f9be30',
secret_key : '',
session_key : '',
uid : '',
auth_token : '',
getting_session : false,
url : {
root : this.FB_ROOT,
api : this.FB_API,
login : this.FB_ROOT + 'login.php',
logout : this.FB_ROOT + 'logout.php', // POST confirm=1
search : this.FB_ROOT + 's.php', // q=searchString
profile : this.FB_ROOT + 'profile.php', // id
poke : this.FB_ROOT + 'poke.php', // id
message : this.FB_ROOT + 'message.php', // id, subject, msg
addfriend : this.FB_ROOT + 'addfriend.php', // id
photos : this.FB_ROOT + 'photos.php', // id
photo_search : this.FB_ROOT + 'photo_search.php', // id
wall : this.FB_ROOT + 'wall.php', // id
notes : this.FB_ROOT + 'notes.php' // id
},
getPref : function(p) {
return this.pref.getFBPref(p);
},
setPref : function(p, v) {
this.pref.setFBPref(p, v);
},
init : function(pref) {
this.pref = pref;
if (this.getPref("session_key") != '')
this.session_key = this.getPref("session_key");
if (this.getPref("secret_key") != '')
this.secret_key = this.getPref("secret_key");
if (!this.isLogedIn()) {
return;
} else {
this.validateAuth();
}
},
isLogedIn : function() {
return !(this.session_key == '' || this.secret_key == '');
},
validateAuth : function() {
var query = [];
query.push('api_key=' + this.api_key);
query.push('format=JSON');
query.push('v=1.0');
query.push('session_key=' + this.session_key);
query.push('call_id=' + this.getCallId());
query.push('method=facebook.users.getLoggedInUser');
query.sort();
query.push('sig=' + md5(query.join('') + this.secret_key));
var self = this;
this.postQuery(this.url.api, query.join('&'), function() {
if (self.xmlHttp == null)
return;
if (self.xmlHttp.readyState == 4) {
if (self.xmlHttp.status != 200) {
self.xmlHttp = null;
return;
}
var s = self.xmlHttp.responseText;
if (s == null) {
return "Couldn't connect";
} else {
var d = JSON.parse(s);
// com.VidBar.__d(s);
self.getNotifications();
if (d.error_code) {
self.setPref("session_key", "");
self.setPref("secret_key", "");
self.session_key = "";
self.secret_key = "";
} else {
self.getNotifications();
}
}
}
});
},
/*
* # AUTHORIZATION
*/
startAuthorization : function() {
var query = [];
query.push('api_key=' + this.api_key);
query.push('v=1.0');
query.push('format=JSON');
query.push('method=facebook.auth.createToken');
query.sort();
query.push('sig=' + md5(query.join('') + this.fsecret_key));
var self = this;
this.postQuery(this.url.api, query.join('&'), function() {
if (self.xmlHttp == null)
return;
if (self.xmlHttp.readyState == 4) {
if (self.xmlHttp.status != 200) {
self.xmlHttp = null;
return;
}
var s = self.xmlHttp.responseText;
if (s == null) {
return "Couldn't connect";
} else {
var d = JSON.parse(s);
self.auth_token = d;
self.openLogin(self.url.login + "?api_key="
+ self.api_key + "&v=1.0&auth_token=" + d);
}
}
});
},
getSession : function() {
var query = [];
query.push('api_key=' + this.api_key);
query.push('auth_token=' + this.auth_token);
query.push('format=JSON');
query.push('v=1.0');
query.push('method=facebook.auth.getSession');
query.sort();
query.push('sig=' + md5(query.join('') + this.fsecret_key));
var self = this;
this.postQuery(this.url.api, query.join('&'), function() {
if (self.xmlHttp == null)
return;
if (self.xmlHttp.readyState == 4) {
if (self.xmlHttp.status != 200) {
self.xmlHttp = null;
return;
}
var s = self.xmlHttp.responseText;
if (s == null) {
return "Couldn't connect";
} else {
var d = JSON.parse(s);
self.session_key = d.session_key;
self.uid = d.uid;
self.secret_key = d.secret;
if (d.expires == 0)
self.setPref("session_key", d.session_key);
self.setPref("secret_key", d.secret);
self.getNotifications();
}
}
//self.getting_session = false;
});
},
/*
* # NOTIFICATIONS
*/
getNotifications : function(manual) {
var query = [];
query.push('api_key=' + this.api_key);
query.push('session_key=' + this.session_key);
query.push('call_id=' + this.getCallId());
query.push('format=JSON');
query.push('v=1.0');
query.push('method=facebook.notifications.get');
query.sort();
query.push('sig=' + md5(query.join('') + this.secret_key));
var self = this;
this.postQuery(this.url.api, query.join('&'), function() {
self.proccessNotifications()
});
if (!manual)
setTimeout(function() {
self.getNotifications()
}, 60000);
},
unreads : {
message : '',
pokes : '',
friend_requests : {
len : 0,
first : 0
},
group_invites : {
len : 0,
first : 0
},
event_invites : {
len : 0,
first : 0
}
},
proccessNotifications : function() {
if (this.xmlHttp == null)
return;
if (this.xmlHttp.readyState == 4) {
if (this.xmlHttp.status != 200) {
this.xmlHttp = null;
return;
}
var s = this.xmlHttp.responseText;
if (s == null) {
return "Couldn't connect";
} else {
var d = JSON.parse(s);
if (d.messages.unread > 0
&& d.messages.most_recent != this.unreads.message) {
this.unreads.message = d.messages.most_recent;
}
document.getElementById("vidbar-fb-mail").label = "("
+ d.messages.unread + ")";
if (d.pokes.unread > 0
&& d.pokes.most_recent != this.unreads.pokes) {
this.unreads.pokes = d.pokes.most_recent;
}
document.getElementById("vidbar-fb-poke").label = "("
+ d.pokes.unread + ")";
if (d.friend_requests.length) {
document.getElementById("vidbar-fb-friend").label = "("
+ d.friend_requests.length + ")";
if (d.friend_requests.length != this.unreads.friend_requests.len
|| d.friend_requests[0] != this.unreads.friend_requests.first) {
this.unreads.friend_requests.first = d.friend_requests[0];
this.unreads.friend_requests.len = d.friend_requests.length;
}
} else {
document.getElementById("vidbar-fb-friend").label = "(0)";
}
if (d.group_invites.length) {
document.getElementById("vidbar-fb-group").label = "("
+ d.group_invites.length + ")";
if (d.group_invites.length != this.unreads.group_invites.len
|| d.group_invites[0] != this.unreads.group_invites.first) {
this.unreads.group_invites.first = d.group_invites[0];
this.unreads.group_invites.len = d.group_invites.length;
}
} else {
document.getElementById("vidbar-fb-group").label = "(0)";
}
if (d.event_invites.length) {
document.getElementById("vidbar-fb-event").label = "("
+ d.event_invites.length + ")";
if (d.event_invites.length != this.unreads.event_invites.len
|| d.event_invites[0] != this.unreads.event_invites.first) {
this.unreads.event_invites.first = d.event_invites[0];
this.unreads.event_invites.len = d.event_invites.length;
}
} else {
document.getElementById("vidbar-fb-event").label = "(0)";
}
}
}
},
openURLInNewTab : function(url) {
com.VidBar.__d("FB: openURLInNewTab");
var browser = window.getBrowser();
var tab = browser.addTab(url);
setTimeout(function(b, t) {
b.selectedTab = t;
}, 0, browser, tab);
},
openMail : function() {
if (!this.isLogedIn()) {
this.startAuthorization();
return;
}
this.openURLInNewTab("http://www.facebook.com/inbox/");
},
openFriend : function() {
if (!this.isLogedIn()) {
this.startAuthorization();
return;
}
this
.openURLInNewTab("http://www.facebook.com/reqs.php");
},
openEvent : function() {
if (!this.isLogedIn()) {
this.startAuthorization();
return;
}
this.openURLInNewTab("http://www.facebook.com/reqs.php");
},
openGroup : function() {
if (!this.isLogedIn()) {
this.startAuthorization();
return;
}
this.openURLInNewTab("http://www.facebook.com/reqs.php");
},
openPoke : function() {
if (!this.isLogedIn()) {
this.startAuthorization();
return;
}
this.openURLInNewTab("http://www.facebook.com/home.php");
},
/*
* UTILITY FUNCTIONS
*/
postQuery : function(url, query, onSuccess) {
this.xmlHttp = new XMLHttpRequest();
this.xmlHttp.onreadystatechange = onSuccess;
this.xmlHttp.open("POST", url);
this.xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
this.xmlHttp.setRequestHeader("Cache-Control", "no-cache");
this.xmlHttp.setRequestHeader("Accept", "text/xml");
this.xmlHttp.send(query);
},
openLogin : function(url) {
com.VidBar.__d("FB: openLogin");
var tab = gBrowser.addTab(url);
var browser = gBrowser.getBrowserForTab(tab);
gBrowser.selectedTab = tab;
var self = this;
this.onLoginLoad = function() {
self.checkLogin(browser.contentDocument, browser);
};
browser.addEventListener("load", this.onLoginLoad, true);
},
checkLogin : function(doc, browser) {
com.VidBar.__d("FB: checkLogin: " + doc.location.toString());
if (this.isLogedIn())
return;
// if (this.getting_session==true)
// return;
if (doc.location.toString()
.indexOf("https://ssl.facebook.com/desktopapp.php") != -1) {
if(this.onLoginLoad)
browser.removeEventListener("load", this.onLoginLoad, true);
//this.getting_session=true;
this.getSession();
}
},
getCallId : function() {
return (new Date()).getTime();
}
};